<?php
/**
 * @version : 1.0
 * @package : lib
 * @subpackage  : form
 * @todo:  class base Form. process action in Form
*/
class CForm {
	public $layout_file_name;

	/**
	 * Constructor
	 *
	 * @param string $layout_file_name
	 */
	function __construct($layout_file_name) {
		$this->layout_file_name = $layout_file_name;
	}

	/**
	 * Enter description here...
	 *
	 * @param unknown_type $data
	 * @return unknown
	 */
	function &prepare_edit($data) {
		if(is_array($data)) {
			foreach($data as $key=>$value) {
				if(!isset($_POST[$key])) {
					$_POST[$key] = $value;
				}
			}
		} elseif($data and request('cmd')=='edit') {
			$this->prepare_edit(CTable($data)->select(EId(request('id'))));
		}
		return $this;
	}

	/**
	 * Enter description here...
	 *
	 * @param unknown_type $data
	 * @param unknown_type $fields
	 * @param unknown_type $input_types
	 * @return unknown
	 */
	function &render_custom_data(&$data, &$fields, &$input_types) {
		foreach($fields as $field) {
			if(isset($input_types[$field['type']]) and isset($input_types[$field['type']]['select-code']) and isset($data[$field['name']])) {
				$value = $data[$field['name']];
				eval($input_types[$field['type']]['select-code']);
				$data[$field['name']] = $value;
			}
		}
		return $this;
	}

	function &get_custom_data_source($data_source) {
		return $this;
	}

	/**
	 * Parse layout from template
	 *
	 * @param unknown_type $params
	 * @param unknown_type $extra
	 * @return unknown
	 */

	function &parse($params = array(), $extra = '') {
		//????? Enable below line for show module name
//		var_dump(module()->name, module()->id, $this->layout_file_name);
	  $code = @crc32($extra);
		$cache_name = CSystem::$form_cache_dir.str_replace(array('/',':'), '-', $this->layout_file_name).$code.'.php';
		$params['root_url'] = CPortal::$root_url;
		$params['static_url'] = CPortal::$static_url;
		$params['currency'] = CURRENCY;
		$module = module();
		$params['mid'] = $module->id;

		if(DEBUG or !file_exists($cache_name)) {			
			$cache = CFormLayout(str_replace('{module()->id}', '{mid}', file_get_contents($this->layout_file_name)))->parse().$extra;
			$code = 'SYS.using ('.
				CString(array(
					'name'=>'<?php echo $module->name;?>',
					'id'=>'<?php echo $params[\'mid\'];?>',
					'parent_id'=>'<?php echo $module->parent_id;?>'))->json();
			$cache = str_replace(
				array('SYS.moduleReady(', 'SYS.using()','SYS.using(',),
				array('SYS.moduleReady(<?php echo $params[\'mid\'];?>,' ,$code.')',$code.','), $cache);
			
			$script = CForm::strip_script($cache);
			//$script = CForm::strip_script($cache);
			if(@COMPRESS_HTML){
				require_once ROOT_DIR.'lib/utils/CMinifyHTML.php';
				$cache = CMinifyHTML::minify($cache);
			}
			if (null!=$script) $cache .= "<?php ob_start(); ?>{$script}<?php return ob_get_clean() ?>";
			file_put_contents($cache_name, $cache);
			//file_put_contents($cache_name, $cache);
		}

		$script = require($cache_name);
		if(/*!request('module') && */isset($script) && $script!==1) {
			//portal()->script .= ';'.$script;
			// Remove excess ; in javascript - Khoanb 25.07.2009
			portal()->script .= $script;
		}
		return $this;
	}

	static function strip_script(&$cache) {
		$code = '';
		$script = '';
		$offset = 0;
		while(($pos1 = strpos($cache, '<script', $offset))!==false) {
			if($pos2 = strpos($cache, '>', $pos1)) {
				while($pos2 and $cache{$pos2-1} == '?') {
					$pos2 = strpos($cache, '>', $pos2+1);
				}
				if ($pos2 and $pos3 = strpos($cache, '</script>', $pos2)) {
					$pos3 += strlen('</script>');
					$nextcode = substr($cache, $offset, $pos1-$offset);

					$code .= $nextcode;

					if(strpos(substr($cache, $pos1, $pos2-$pos1),'src')) {
						$script .= '</script>'.substr($cache, $pos1, $pos3-$pos1).'<script type="text/javascript">';
					} else {
						$script .= substr($cache, $pos2+1, $pos3-strlen('</script>')-$pos2-1);
					}
					$offset = $pos3;
				}
				else
					break;
			} else
				break;
		}
		$code .= substr($cache, $offset);

		/*
		$lines = explode("\n", $script);
		$cssScript = '';
		foreach ($lines as &$line) {
		  if (preg_match('@css\'\\);@', $line)) {
		    $cssScript.= $line;
		    $line = '';
		  }
		}
		if ($cssScript!='') $cssScript = '<script type="text/javascript">'.$cssScript.'</script>';
		$cache = $cssScript .$code;

		return join("\n", $lines);*/
		$cache = $code;
		return $script;
	}
}


function CForm($layout_file_name)
{
	return new CForm($layout_file_name);
}
function image($url)
{
	if(strpos($url, 'http://'))
	{
		return $url;
	}
	if(strpos($url, '/upload/') === 0)
	{
		return FILE_SERVER_URL.$url;
	}
	if(strpos($url, 'upload/') === 0)
	{
		return FILE_SERVER_URL.'/'.$url;
	}
	if(!$url)
	{
		return CPortal::$root_url.portal()->skin().'images/no-image.jpg';
	}
	return $url;
}
?>